package com.superzqb.asus.superzqbFragment.superzqbJobShop.Home;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.superzqb.asus.Api.CampusShopApi;
import com.superzqb.asus.Api.ShopApi;
import com.superzqb.asus.superzqb.R;
import com.superzqb.asus.superzqbFragment.superzqbJobShop.Bean.ShopNameBean;
import com.superzqb.asus.superzqbUtil.GeneralFun;
import com.superzqb.asus.superzqbUtil.LoadDialog;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by asus on 2017/1/17.
 */

public class CampusHome extends Activity implements View.OnClickListener{
    private ImageView back;
    private Context context;
    private TextView title,name;
    private SharedPreferences sp;
    private String uid;
    private Dialog loadingDialog;


    private LinearLayout jygl,wlgl,khfw,xdmb,spgl,tjsp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.campus_home);

        initView();//初始化视图
        loadingDialog= LoadDialog.getInstance(context,"正在加载");
        loadingDialog.show();
        getData();
    }

    private void initView() {

        context=CampusHome.this;
        sp = getSharedPreferences("uinfo", MODE_PRIVATE);
        uid=sp.getString("uid",null);
        title=(TextView)findViewById(R.id.title);
        name=(TextView)findViewById(R.id.name);
        back=(ImageView)findViewById(R.id.back);
        title.setText("我的微创小屋");
        jygl=(LinearLayout)findViewById(R.id.jygl);
        wlgl=(LinearLayout)findViewById(R.id.wlgl);
        khfw=(LinearLayout)findViewById(R.id.khfw);
        xdmb=(LinearLayout)findViewById(R.id.xdmb);
        spgl=(LinearLayout)findViewById(R.id.spgl);
        tjsp=(LinearLayout)findViewById(R.id.tjsp);

        back.setOnClickListener(this);
        jygl.setOnClickListener(this);
        wlgl.setOnClickListener(this);
        khfw.setOnClickListener(this);
        xdmb.setOnClickListener(this);
        spgl.setOnClickListener(this);
        tjsp.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        Intent intent=new Intent();
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.jygl:
                intent.setClass(context,CampusTransactionManagement.class);
                startActivity(intent);
                break;
            case R.id.wlgl:
                intent.setClass(context,CampusLogisticsManagement.class);
                startActivity(intent);
                break;
            case R.id.khfw:
                intent.setClass(context,CampusCustomerService.class);
                startActivity(intent);
                break;
            case R.id.spgl:
                intent.setClass(context,CampusShopManagement.class);
                startActivity(intent);
                break;
            case R.id.tjsp:
                intent.setClass(context,CampusAddShop.class);
                startActivity(intent);
                break;
            case R.id.xdmb:
               /* intent.setClass(context,CampusAddShop.class);
                startActivity(intent);*/
                Toast.makeText(context,"努力开发中",Toast.LENGTH_SHORT).show();
                break;
            default:break;



        }
    }

    public void getData() {
        Retrofit retrofit=new GeneralFun().gsonCreate();
        CampusShopApi api=retrofit.create(CampusShopApi.class);
        Call<ShopNameBean> call=api.getShopName(uid);
        call.enqueue(new Callback<ShopNameBean>() {
            @Override
            public void onResponse(Call<ShopNameBean> call, Response<ShopNameBean> response) {
                if(response.isSuccessful()){
                    ShopNameBean tmp=response.body();
                    if(!tmp.getId().equals("-1")){
                        name.setText(tmp.getName()+"的小店");
                    }else
                        Toast.makeText(context,"店铺名获取失败",Toast.LENGTH_SHORT);
                }
                if(loadingDialog!=null)
                loadingDialog.dismiss();
            }

            @Override
            public void onFailure(Call<ShopNameBean> call, Throwable throwable) {
                if(loadingDialog!=null)
                loadingDialog.dismiss();
                Toast.makeText(context,throwable.getMessage(),Toast.LENGTH_SHORT);
            }
        });
    }
}
